var unirest = require("unirest");
var request = require("request");
var req = unirest("GET", "https://xueqiu.com/v4/statuses/user_timeline.json");
var access_token = '3fc5ceb52ffc5e6c1d9f6b25df851013f9124faa0a1821e71c376d3da7884bc5';

function getData(){
    req.query({
    "page": "1",
    "user_id": "2814876878"
    });

    req.headers({
    "cache-control": "no-cache",
    "Referer": "https://xueqiu.com/u/2814876878",
    "Connection": "keep-alive",
    "Cookie": "device_id=24700f9f1986800ab4fcc880530dd0ed; s=c312le1jw7; bid=6a1bdb02d932ad6d10ace816fea523d6_k43re57j; _ga=GA1.2.1564759540.1602581761; xq_a_token=254d9e45e4973cbfde67be5c174899de289ce1f5; xqat=254d9e45e4973cbfde67be5c174899de289ce1f5; xq_r_token=d2897a1a47200731ed015834ed1f5d794754dae7; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOjUzODcyMDIzNjksImlzcyI6InVjIiwiZXhwIjoxNjA1MTczODYxLCJjdG0iOjE2MDI1ODE4NjE5NzUsImNpZCI6ImQ5ZDBuNEFadXAifQ.oW3yXcbM2pi5FgLzmkv_8MBzaVaASIc_8Tp1M__i7Y-hyFWhe57GPNfAJEoAbfmIrhfC88lhQUhAf944TY_zaesogdNBiXEfzjU5o739H9FnT7-3zSdQTNdSE9LKdxAXvdW5SJUCw_AR4KQh4Xyq-anXIJNLCoMvN4DHMkUp44VH1cN_eXMH23kJ-pVBZXBVoYZMEWPBsx-aRvmS5NIMbqZ5rJY9IlwpEn5j4T6AFPIkYWR19pSchSjwxowmsYDnUb67JhFHpX2uE1H5parHtcyzCoOFz0eJFXCk7iKIHGB9lBVKV_qB1Fw04ftLxgRqfvga8AtCBkiKgeIFeonkiw; xq_is_login=1; u=5387202369; __utma=1.1822410624.1576217876.1576217876.1602581977.2; __utmz=1.1602581977.2.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); acw_tc=2760824b16036828993207491e80b326851c29b4136c2e688102fe823dde32; is_overseas=0; Hm_lvt_1db88642e346389874251b5a1eded6e3=1602581735,1602594726,1603682948; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1603682990",
    "X-Requested-With": "XMLHttpRequest",
    "Cache-Control": "no-cache",
    "Accept": "*/*",
    "elastic-apm-traceparent": "00-91555c250c566ce66e0e41fdc2fa3e23-53185a022e41f804-00",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
    "Accept-Encoding": "gzip, deflate, br",
    "Pragma": "no-cache"
    });


    req.end(function (res) {
    if (res.error) throw new Error(res.error);
    // console.log(res.body)
    doRes(res.body.statuses);
    });
}
var IDS=[];

function doRes(array){
    for (let index = 0; index < array.length; index++) {
        const element = array[index];
        // console.log(element.id);
        var id = element.id;
        if(IDS.indexOf(id)===-1){
            sendData(element.description);
            IDS.push(id);
        }
        
    }

}

function sendData(content,isAtAll){
    content=new Date().toLocaleString()+'    消息如下:\n'+content;
    var options = { 
        method: 'POST',
        url: 'https://oapi.dingtalk.com/robot/send',
        qs: { 
            access_token: access_token 
        },
        headers: 
        { 'Postman-Token': '207df61c-e31b-4d4f-b7b3-aca8931347e5',
            'cache-control': 'no-cache',
            'Content-Type': 'application/json' },
        body: {
            "msgtype": "text", 
            "text": {
                "content": content
            },
            "at": {
                "isAtAll": !!isAtAll
            }
        },
        json: true
    };

    request(options, function (error, response, body) {
        if (error) throw new Error(error);
        // console.log(body);
    });

}



setInterval(() => {
    getData(); 
}, 8000);